# -*- coding: utf-8 -*-

import requests,json,os,urllib,string,sys,scrapy,shutil
from g0a.sqlHelper import *

reload(sys)
sys.setdefaultencoding('utf8')

class ZqSpider(scrapy.Spider):
    name = 'zq'
    allowed_domains = ['90a.cc']
    handle_httpstatus_list = [301,403,404]
    offset=7
    url='http://info.90a.cc/index.php?c=hoo&a=getTp&teamid='
    endurl='&leagueid=&type=tdl&year='
    start_urls = [url+str(offset)+endurl]
    def parse(self, response):
        if response.status==404 or response.status==301 or response.status==403:
            print self.url+str(self.offset)+'------页面不存在'
        else:
            try:
                res=response.body
                res=res[res.index("["):res.index("]")+1]
                try:
                    res=eval(res)
                except:
                    res=res+"']"
                    res=eval(res)
                cn=res[1]
                big=res[2]
                en=res[3]
                url=r"http://info.90a.cc/live/data/info/image/team/"+res[4]
                fn=url[len(url)-4:]
                if fn!='.jpg' and fn!='.png' and fn!='.gif':
                    self.save_data(en,cn,big,'')
                else:
                    self.save_data(en,cn,big,en+fn)
                    self.save_img(url,en)
            except Exception,e:
                traceback.print_exc()
                print str(self.offset)+'解析错误！'

        if self.offset < 90623:
            self.offset += 1
        yield scrapy.Request(self.url+str(self.offset)+self.endurl, callback = self.parse,dont_filter=True)

    def save_data(self,en,cn,big,logo):
        print cn+'--'+en+'--'+big+'--'+logo
        if sqlHelper.selectOne("select id from team where cn=%s and typeText=%s",(cn,'足球')):
            if sqlHelper.update("UPDATE team SET source=%s,en=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE en='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),en,cn,'足球')):
                print '更新'+cn+'--en>>'+en
            if sqlHelper.update("UPDATE team SET source=%s,big=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE big='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),big,cn,'足球')):
                print '更新'+cn+'--big>>'+big
            if sqlHelper.update("UPDATE team SET source=%s,logo=%s WHERE cn=(SELECT cn FROM (SELECT * FROM team WHERE logo='' and cn=%s and typeText=%s) AS t)",(self.url+str(self.offset),logo,cn,'足球'    )):
                print '更新'+cn+'--logo>>'+logo

        else:
            sqlHelper.update("insert into team(type,typeText,cn,en,logo,big,source) values(%s,%s,%s,%s,%s,%s,%s)",(30,'足球',cn,en,logo,big,self.url+str(self.offset)+self.endurl))
        return True
    
    def save_img(self,img_url,file_name,file_path='zq'):
        try:
            if not os.path.exists(file_path):
                print '文件夹',file_path,'不存在，重新建立'
                #os.mkdir(file_path)
                os.makedirs(file_path)
            #获得图片后缀
            file_suffix = os.path.splitext(img_url)[1]
            #拼接图片名（包含路径）
            filename = '{}{}{}{}'.format(file_path,os.sep,file_name,file_suffix)
            headers = {
                        'Pragma': "no-cache",
                        'Accept-Encoding': "gzip, deflate",
                        'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7",
                        'Upgrade-Insecure-Requests': "1",
                        'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36",
                        'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                        'Cache-Control': "no-cache",
                        'Connection': "keep-alive",
                    }
            r = requests.get(img_url, headers=headers, stream=True)
            if r.status_code == 200:
                with open(filename, 'wb') as f:
                    r.raw.decode_content = True
                    shutil.copyfileobj(r.raw, f)
#        except Error as e:
#            print '文件下载失败！',e
        except Exception as e:
            print '错误 ：',e

